<html lang="zh-cn">
<head runat="server">
    <title></title>
    <meta charset="utf-8" />
<!--    <script type="text/JavaScript" src="js/lock.js"></script>-->

<!--    <script type="text/javascript" src="js/My97DatePicker/WdatePicker.js"></script>-->
<!--    <script type="text/javascript" src="js/calendar/common.js"></script>-->
<!--    <script type="text/javascript" src="js/calendar/calendar_core.js"></script>-->
<!--    <script type="text/javascript" src="js/calendar/calendar.js"></script>-->
<!--    <link rel="stylesheet" type="text/css" href="js/calendar/calendar.css" />-->
    <script>
        function formatDateTime(date, format) {
            // 兼容IE
            var time;
            if (!date) return "";
            if (typeof date === 'string') {
                time = new Date(date.replace(/-/g, '/').replace(/T|Z/g, ' '));
            } else if (typeof date === 'object') {
                time = new Date(date);
            }
            var o = {
                "M+": time.getMonth() + 1, //月份
                "d+": time.getDate(), //日
                "h+": time.getHours(), //小时
                "m+": time.getMinutes(), //分
                "s+": time.getSeconds(), //秒
                "q+": Math.floor((time.getMonth() + 3) / 3), //季度
                "S": time.getMilliseconds() //毫秒
            };
            if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (time.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in o)
                if (new RegExp("(" + k + ")").test(format)) format = format.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return format;
        }
        /***********************************************************************
         * 判断一个字符串是否为合法的日期格式:YYYY-MM-DD HH:MM:SS
         * 或 YYYY-MM-DD 或 HH:MM:SS
         */
        function isDateStr(ds) {
            parts = ds.split(' ');
            if (parts.length != 2) return false;
            if (isDatePart(parts[0]) == true && isTimePart(parts[1])) {
                return true;
            } else {
                return false;
            }
        }
        /***********************************************************************
         * 判断一个字符串是否为合法的日期格式:YYYY-MM-DD
         */
        function isDatePart(dateStr) {
            var parts;
            if (dateStr.indexOf("-") > -1) {
                parts = dateStr.split('-');
            } else if (dateStr.indexOf("/") > -1) {
                parts = dateStr.split('/');
            } else {
                return false;
            }
            if (parts.length < 3) {
                //日期部分不允许缺少年、月、日中的任何一项
                return false;
            }
            for (i = 0; i < 3; i++) {
                //如果构成日期的某个部分不是数字，则返回false
                if (isNaN(parts[i])) {
                    return false;
                }
            }
            y = parts[0];//年
            m = parts[1];//月
            d = parts[2];//日
            if (y > 3000) {
                return false;
            }
            if (m < 1 || m > 12) {
                return false;
            }
            switch (d) {
                case 29:
                    if (m == 2) {
                        //如果是2月份
                        if ((y / 100) * 100 == y && (y / 400) * 400 != y) {
                            //如果年份能被100整除但不能被400整除 (即闰年)
                        } else {
                            return false;
                        }
                    }
                    break;
                case 30:
                    if (m == 2) {
                        //2月没有30日
                        return false;
                    }
                    break;
                case 31:
                    if (m == 2 || m == 4 || m == 6 || m == 9 || m == 11) {
                        //2、4、6、9、11月没有31日
                        return false;
                    }
                    break;
                default:
            }
            return true;
        }
        /***********************************************************************
         * 判断一个字符串是否为合法的时间格式:HH:MM:SS
         */
        function isTimePart(timeStr) {
            var parts;
            parts = timeStr.split(':');
            if (parts.length < 2) {
                //日期部分不允许缺少小时、分钟中的任何一项
                return false;
            }
            for (i = 0; i < parts.length; i++) {
                //如果构成时间的某个部分不是数字，则返回false
                if (isNaN(parts[i])) {
                    return false;
                }
            }
            h = parts[0];//年
            m = parts[1];//月
            if (h < 0 || h > 23) {
                //限制小时的范围
                return false;
            }
            if (m < 0 || h > 59) {
                //限制分钟的范围
                return false;
            }
            if (parts.length > 2) {
                s = parts[2];//日
                if (s < 0 || s > 59) {
                    //限制秒的范围
                    return false;
                }
            }
            return true;
        }
        //  格式化日期的显示:
        function FormatDateTime(now) {
            var res = "" + now.getFullYear() + "-" + padZero(now.getMonth() + 1) +
                "-" + padZero(now.getDate()) + " " + padZero(now.getHours()) + ":" +
                padZero(now.getMinutes()) + ":" + padZero(now.getSeconds());
            return res;
        }
        function padZero(num) {
            var result;
            if (num < 10) {
                result = "0" + num;
            } else {
                result = "" + num;
            }
            return result;
        }
        function Init() {
            return window.JeaceLock.Open();
        }
        //   结束控件使用,一般不需要特别调用，关闭页面时，系统自动结束控件
        function Close() {
            return window.JeaceLock.Close();
        }

        /**
         * 续房
         * */
        function ExtendCard() {
            if (ReadCard(true)) {
                if (WriteCard(true)) {
                    alert(document.getElementById('txtRoomNo').value + "续房成功！")
                }
                return true;
            }

            return false;
        }
        //   写卡
        //   RoomNo      字符串，房间号
        //   Build       整型，楼栋号
        //   Floor       整型，楼层号
        //   CheckIn     日期时间型，入住时间
        //   CheckOut    日期时间型，预离时间
        //   IsCopy      布尔型，是否为同住客户 True 是  Flase 否
        //   AuxInfoI    字符串，辅助房号，部门锁具需要使用，可以根据房间号取得，并放在隐藏的文本域中取得
        //   返回写卡是否成功
        //   参数辅助房号,不用可以传入''
        function WriteCard(isExtend) {
            var Build = Number(document.getElementById("txtBuild").value);
            var Floor = Number(document.getElementById("txtFloor").value);
            var CheckIn = document.getElementById("txtStartInVisible").value;
            var RoomNo = document.getElementById("txtRoomNo").value;
            var AuxInfoI = document.getElementById("txtRoomNoAux").value;
            var CheckOut = document.getElementById("txtEndVisible").value + ' ' + document.getElementById("DDL_Hour").value + ':' + document.getElementById("DDL_Minute").value + ':00';
            var IsCopy = document.getElementById("cbAddCard").checked;
            if (isDateStr(CheckIn) == false && !isExtend) {
                alert('入住时间不正确！');
                return false;
            }
            if (isDateStr(CheckOut) == false) {
                alert('预离时间错误不正确！');
                return false;
            }

            if (new Date(CheckOut.replace(/-/g, '/').replace(/T|Z/g, ' ')) - new Date() <= 30 * 60 * 1000) {
                alert("截止时间必须大于当前30分钟！");
                return false;
            }


            var userType = document.getElementById('hidUserType').value;
            //走新的程序?
            if (userType == 1) {
                try {
                    var xhr = new XMLHttpRequest();
                    xhr.open('POST', 'http://127.0.0.1:3000/Lock/WriteCard', false);
                    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
                    var params = 'CheckOut=' + formatDateTime(CheckOut, 'yyMMddhhmm') + '&LockCode=' + AuxInfoI + '&CheckIn=' + formatDateTime(CheckIn, 'yyMMddhhmm')
                    //console.log(params);

                    xhr.send(params);

                    if (xhr.readyState == 4) {
                        if (xhr.status == 200) {
                            if (!isExtend)
                                alert("写卡成功！");
                        }
                        else if (xhr.status == 500) {
                            alert(xhr.responseText);
                            return false;
                        }
                    }
                    return true;
                } catch (err) {
                    //console.log(err);
                    alert("操作不成功！");
                    return false;
                }
            }
            else {
                if (window.JeaceLock.WriteCard(RoomNo, Build, Floor, CheckIn, CheckOut, IsCopy, AuxInfoI) == true) {

                    if (!isExtend)
                        alert('写卡成功！');
                    document.all.btnWrite.click();
                    return true;
                }
                else {
                    alert('写卡失败！');
                    return false;
                }
            }
        }
        //   读卡
        //   注意:读卡并不是每种类型的锁具都支持
        //         且不是每种支持的锁具都能够读出所有的数据
        //         请根据不同的锁具的说明，进行操作
        //         详见Winform测试程序
        function ReadCard(isExtend,isHttps) {
            var userType = document.getElementById('hidUserType').value;
            //走新的程序?
            if (userType == 1) {
                try {
                    var xhr = new XMLHttpRequest();
                    xhr.open('POST', 'http'+(isHttps?'s':'')+'://localhost:3000/Lock/ReadCard', false);
                    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
                    // xhr.setRequestHeader('cache-control', 'no-cache')
                    xhr.send('');

                    if (xhr.readyState == 4) {
                        if (xhr.status == 200) {
                            var rs = xhr.responseText.split(',');
                            if (!isExtend) {
                                //alert(rs[0]);
                                var dateStr = rs[2];
                                alert("房间号：" + rs[0] + "\n离店时间：" + dateStr.substring(0, 2) + "年" + dateStr.substring(2, 4) + "月" + dateStr.substring(4, 6) + "日 " + dateStr.substring(6, 8) + ":" + dateStr.substring(8, 10));
                            }
                            document.getElementById('txtRoomNo').value = rs[0];
                            document.getElementById('txtRoomNoAux').value = rs[1];
                            document.getElementById('txtBuild').value = xhr.responseText.substr(0, 2);
                            document.getElementById('txtFloor').value = xhr.responseText.substr(2, 2);
                        }
                        else if (xhr.status == 500) {
                            alert(xhr.responseText);
                            return false;
                        }
                    }
                    return true;
                } catch (err) {
                    //console.log(err);
                    //alert(err)
                    alert("操作不成功！");
                    return false;
                }
            }
            else {
                var roomNo2 = window.JeaceLock.ReadCard_RoomNO();
                // alert(roomNo2);
                var tempArr = roomNo2.split(",");
                var lockcode = tempArr[0];
                var deptTime = tempArr[1];
                document.getElementById("txtRoomNo").innerText = lockcode;
                document.getElementById("txtEndVisible").innerText = deptTime;
                return false;
                var RoomNo = new Object();
                var Build = new Object();
                var Floor = new Object();
                var CheckIn = new Object();
                var CheckOut = new Object();
                var RoomNoAux = new Object();
                RoomNo.value = "         ";
                Build.value = 0;
                Floor.value = 0;
                CheckIn.value = new Date();
                CheckOut.value = new Date();
                RoomNoAux.value = "   ";
                if (window.JeaceLock.ReadCard(RoomNo, Build, Floor, CheckIn, CheckOut, RoomNoAux) == true) {
                    /*  可以将读取到的数据进行处理,如赚值显示等
                    alert('房间号:' + RoomNo.value.toString());
                    alert('楼栋号:' + Build.value.toString());
                    alert('楼层号:' + Floor.value.toString());
                    alert('入住时间:' + FormatDateTime(CheckIn.value));
                    alert('预离时间:' + FormatDateTime(CheckOut.value));
                    alert('辅助房号:' + RoomNoAux.value.toString());
                    */
                    if (Build && RoomNo) {
                        var roomNo = Build.value.toString() + RoomNo.value.toString();
                        if (!isExtend)
                            alert(roomNo);
                        document.getElementById('labRoomNo').text = roomNo;
                    }
                    return true;
                }
                else
                    return false;
            }
        }
        function CancelCard() {
            var userType = document.getElementById('hidUserType').value;
            //走新的程序?
            if (userType == 1) {
                try {
                    var xhr = new XMLHttpRequest();
                    xhr.open('POST', 'http://127.0.0.1:3000/Lock/CancelCard', false);
                    // POST请求需要设置此参数
                    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
                    xhr.send();

                    if (xhr.readyState == 4) {
                        if (xhr.status == 200) {
                            alert('注消成功！');
                        }
                        else if (xhr.status == 500) {
                            alert(xhr.responseText);
                            return false;
                        }
                    }
                    return true;
                }
                catch (err) {
                    //console.log(err);
                    alert("操作不成功?");
                    return false;
                }
            }
            else {
                if (window.JeaceLock.DeleteCard() == true) {
                    alert('注消成功！');
                    return true;
                }
                else
                    return false;
            }
        }
    </script>

</head>
<body style="margin: 0px; background-color: #DFFFDF;">
<form id="form1" runat="server">


    <div class="main" style="width: 1400px; float: left; margin-left: 10px;">
        楼栋：
        <input type="text" ID="txtBuild" TextMode="SingleLine" Columns="3" runat="server" Text="1" BorderWidth="0px" MaxLength="4" />
        楼层:
        <input type="text" ID="txtFloor" TextMode="SingleLine" Columns="3" runat="server" Text="1" BorderWidth="0px" MaxLength="4" />
        &nbsp;&nbsp;            &nbsp;&nbsp;         显示房号:
        <input type="text" ID="txtRoomNo" TextMode="SingleLine" Columns="5" runat="server" Text="01" ClientID="txtRoomNo" MaxLength="19" />
        &nbsp;&nbsp;  物理房号:  (从厂家接口文档获取)



        <input type="text" ID="txtRoomNoAux" TextMode="SingleLine" Columns="5" runat="server" Text="010201" MaxLength="19" />

        <br />
        <br />
        起始 ：
        <input type="text" ID="txtStartInVisible" TextMode="SingleLine" Columns="17" runat="server" Font-Size="22px" Text="2021-01-02 13:00:00" MaxLength="19" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" class="Wdate" />

        截止：

        <asp:Label ID="label6" runat="server" Text="预离:" Font-Size="15px" Visible="false" />
        <input type="text" ID="txtEndVisible" TextMode="SingleLine" Columns="12" runat="server" Text="2023-05-29"   BorderWidth="1px" Height="30px" ClientID="txtEndVisible" Font-Names="Arial, Helvetica, sans-serif;" MaxLength="19" onfocus="calendar();" />&nbsp;




        <asp:DropDownList ID="DDL_Hour" runat="server">
            <asp:ListItem Value="12" Selected="True">12点</asp:ListItem>

        </asp:DropDownList>



        <asp:DropDownList ID="DDL_Minute" runat="server">
            <asp:ListItem Value="30" Selected="True">30分</asp:ListItem>
        </asp:DropDownList>



        <asp:CheckBox ID="cbAddCard" runat="server" Text="加卡" Style="zoom: 110%" ForeColor="#666666" Font-Size="12px" />&nbsp;&nbsp;
        <asp:Button ID="btnWrite" runat="server" Text="制卡" OnClientClick="return WriteCard()" Font-Size="18px" Font-Bold="true" Font-Names="Arial, Helvetica, sans-serif;" />&nbsp;&nbsp;
        <asp:Button ID="BtnCardOperate" runat="server" Text="退卡" OnClientClick="return CancelCard()" Font-Size="18px" />&nbsp;&nbsp;
        <input type="button" value="读卡" ID="BtnRead"  Text="读卡" onclick="ReadCard()" Font-Size="18px" Font-Bold="true" Font-Names="Arial, Helvetica, sans-serif;"/>&nbsp;&nbsp;
        <input type="button" value="读卡" ID="BtnRead"  Text="读卡2" onclick="ReadCard(false,true)" Font-Size="18px" Font-Bold="true" Font-Names="Arial, Helvetica, sans-serif;"/>&nbsp;&nbsp;
        <asp:Button ID="BtnExtend" runat="server" Text="续房" OnClientClick="return ExtendCard()" Font-Size="18px" Font-Bold="true" Font-Names="Arial, Helvetica, sans-serif;" />

        <input type="hidden" ID="hidUserType" runat="server" Value="1" />

    </div>
</form>
<div style="display: none">
    <object id="JeaceLock" classid='clsid:6D932DA3-32AD-46C3-A365-DFC2CB3AFBD4'></object>
</div>
</body>
</html>
